package com.etc.my.service.impl;

import com.etc.my.dao.UserDao;
import com.etc.my.dto.CarFavoriteDto;
import com.etc.my.entity.User;
import com.etc.my.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao dao;

    public UserDao getDao() {
        return dao;
    }

    public void setDao(UserDao dao) {
        this.dao = dao;
    }



    @Override
    public List<User> getAllUser() {
        return dao.queryAllUser();
    }

    @Override
    public boolean userRegister(User user) {
        if(user == null | user.getUser_name() == null | user.getUser_pwd() == null | user.getUser_phone() == null){
            return false;
        }
        return dao.addUser(user);
    }

    @Override
    public User userLogin(String user_phone, String user_pwd) {
        if (user_phone == null | user_pwd == null){
            return null;
        }
        return dao.getOneUserByPhoneAndPwd(user_phone,user_pwd);
    }

    @Override
    public boolean updateUser(User user) {
        if(user == null | user.getUser_id() == null){
            return false;
        }
        return dao.updateUserById(user);
    }

    @Override
    public User queryUserById(Integer user_id) {
        if(user_id == null){
            return null;
        }
        return dao.queryUserById(user_id);
    }

    @Override
    public List<CarFavoriteDto> getFavoriteCar(Integer user_id,Integer page,Integer size) {
        return dao.getFavoriteCar(user_id,page,size);
    }

    @Override
    public Long getFavoriteCount(Integer user_id) {
        return dao.getFavoriteCount(user_id);
    }


    /**
     * 根据电话判断用户是否存在
     *
     * @return false 没有存在的 true 有存在
     */
    @Override
    public boolean queryUserByPhone(String user_phone) {
        User user = dao.queryUserByPhone(user_phone);
        if(user != null){
            return true;
        }
        else return false;
    }
}
